package com.contain.minio.service;

import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: tianchao
 * @Date: 2021/8/21 22:54
 * @Description:
 */
@Service
public class BucketOperationsService {

    @Autowired
    private MinioClient minioClient;

    public void makeBucket(){
        try {
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build())) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("my-bucketname").build());
                System.out.println("my-bucketname is created successfully");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public List<Bucket> listBuckets(){
        List<Bucket> bucketList = null;
        try {
            bucketList = minioClient.listBuckets();
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Bucket bucket : bucketList) {
            System.out.println(bucket.creationDate() + ", " + bucket.name());
        }
        return bucketList;
    }
    public boolean bucketExists(String bucketName){
        boolean b = false;
        try {
            b = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

    public List<Item> listObjects(String startAfter,String prefix, Integer maxKeys){
        List<Item> list = new ArrayList<>();
        ListObjectsArgs.Builder builder = ListObjectsArgs.builder()
                .bucket("dockerminio");
        if (!StringUtils.isEmpty(startAfter)){
            builder.startAfter(startAfter);
        }
        if (!StringUtils.isEmpty(prefix)){
            builder.prefix(prefix);
        }
        if (maxKeys!=null){
            builder.maxKeys(maxKeys);
        }
        builder.useApiVersion1(false);
        builder.includeUserMetadata(true);
        builder.recursive(true);

        Iterable<Result<Item>> results =
                minioClient.listObjects(builder.build() );


        for (Result<Item> result : results) {
            Item item = null;
            try {
                item = result.get();
                System.out.println(item.userMetadata());
                System.out.println(item.etag());
                System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());
                list.add(item);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }



    public void removeBucket(String bucketName){
        try {
            minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
